#include <panic.h>
#include <isr.h>
#include <monitor.h>

extern void panic_header()
{
    change_colors(4, 15);
	monitor_clear();
    printf("A error occured in Ensemble and has stopped to protect your computer\n\n");
    printf("***Technical Information\n");
}

extern void panic(const char *message, const char *file, u32int line)
{
    // We encountered a massive problem and have to stop.
    asm volatile("cli"); // Disable interrupts.
	panic_header();
	printf("PANIC: %s (%s, %d)\n", message, file, line);

	// Halt by going into an infinite loop.
    for(;;);
}

extern void panic_registers(const char *message, const char *file, u32int line, registers_t *regs)
{
    // We encountered a massive problem and have to stop.
    asm volatile("cli"); // Disable interrupts.
        panic_header();
		printf("***LM: 0x%x (0x%x, 0x%x, 0x%x, 0x%x)\n", regs->int_no, regs->eax, regs->ebx, regs->ecx, regs->edx);
		printf("%s (%s, %d)\n", message, file, line);
		
		printf("\n***Additional Registers\n");
		printf("edi: 0x%x esi: 0x%x ebp: 0x%x\n", regs->edi, regs->esi, regs->ebp);
		printf("esp: 0x%x ecx: 0x%x err_code: 0x%x\n", regs->esp, regs->ecx, regs->err_code);
		printf("eip: 0x%x ss: 0x%x cs: 0x%x\n", regs->eip, regs->ss, regs->cs);
		printf("eflags: 0x%x useresp: 0x%x ds: 0x%x\n", regs->eflags, regs->useresp, regs->ds);
        
		//printf("\n***Loaded Modules\n");

        #ifdef USE_SERIAL_DEBUGGER
        printf("Kernel Debugger Using: COM1 (Port 0x%x, Baud Rate 38400)\n");
		//write_serial("Kernel Debugger Using: COM1 (Port 0x%x, Baud Rate 38400)\r\n");
        for(;;);
		/*{
			char x = read_serial();
			if(strcmp(x, "test\r\n") == 0)
			{
				write_serial("Test Success\r\n");
			}
		}*/
        #else
        for(;;);
        #endif
}


extern void panic_assert(const char *file, u32int line, const char *desc)
{
    // An assertion failed, and we have to panic.
    asm volatile("cli"); // Disable interrupts.
	panic_header();
	printf("ASSERTION FAILED: %s (%s, %d)\n", desc, file, line);

	// Halt by going into an infinite loop.
    for(;;);
}
